#!/usr/bin/python
# -*- coding: UTF-8 -*-

################################################################################
#
# Copyright (c) 2020 openEuler.org, Inc. All Rights Reserved
#
################################################################################
"""
common files

Authors: zhangjianjun
Date:    04/01/2021 11:01 AM
"""

import os
import shutil
import yaml


def mkdirs(file_path, file_flag):
    """
    Create a directory, return the creation success or failure, bool value
    file_flag ==1 Delete all files
    return bool
    """
    path = file_path.strip()
    path = path.rstrip("/")
    isExists = os.path.exists(path)
    if not isExists:
        try:
            os.makedirs(path)
        except OSError:
            return False
    else:
        if file_flag == 1:
            del_list = os.listdir(path)
            for f in del_list:
                file_path = os.path.join(path, f)
                if os.path.isfile(file_path):
                    os.remove(file_path)
                elif os.path.isdir(file_path):
                    shutil.rmtree(file_path)
    return True


def file_isexists(file_path):
    """
    Determine whether the file exists
    return bool
    """
    if os.path.exists(file_path):
        return True
    else:
        return False


def parse_yaml(file_name):
    """
    Parse yaml
    return dict
    """
    with open(file_name) as f:
        yaml_data = yaml.safe_load(f)
        return yaml_data
